package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class EnterGroupActivity extends AppCompatActivity implements View.OnLongClickListener{

	private ActionBar mActionBar = null;

	private ImageView mQRImage = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_enter_group);

		mQRImage = findViewById(R.id.image_qr_code);
		mQRImage.setOnLongClickListener(this);

		mActionBar = findViewById(R.id.actionBarEnterGroup);
		mActionBar.setOnBackClickListener( new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public boolean onLongClick(View v) {
		if(v.getId() == R.id.image_qr_code){
			Resources res = this.getResources();
			BitmapDrawable d = (BitmapDrawable) res.getDrawable(R.drawable.qr_enter_group);
			Bitmap img = d.getBitmap();

			String fn = "image_test.png";
			String path = this.getFilesDir() + File.separator + fn;
			try{
				OutputStream os = new FileOutputStream(path);
				img.compress(Bitmap.CompressFormat.PNG, 100, os);
				os.close();
				ToastUtils.showLongToast(this, path);
			}catch(Exception e){
				Log.e("TAG", "", e);
			}

			try {
				MediaStore.Images.Media.insertImage(getContentResolver(), path, "title", "description");
				sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(path))));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}

			return true;
		}

		return false;
	}
}